package fictional.GUI;

import java.io.IOException;
import javax.swing.JButton;

import org.jdesktop.application.Application;

import fictional.Common.CharacterItem;
import fictional.Common.FriendRequestItem;
import fictional.Common.FriendRequests;
import fictional.Common.IBL;
import fictional.Common.Response;
import fictional.Common.ShortCharacterItem;

/**
 * This task adds and removes friends from the logged in user's
 * friends list.
 */
public class AddRemoveFriendTask extends DefaultTask{
	
	public static final int REQUEST_TASK = 0;
	public static final int ADD_TASK = 1;
	public static final int REMOVE_TASK = 2;
	public static final int IGNORE_TASK = 3;
	
	private int taskNumber = -1;
	private CharacterItem character;
	private ShortCharacterItem friend;
	private FriendRequests friendRequests;
	private FriendRequestItem friendRequestItem;
	private JButton button;
	private JButton secondButton;

	public AddRemoveFriendTask(Application application) {
		super(application);
	}
	
	public void setRequestTask(IBL BL, GUI gui, CharacterItem character, ShortCharacterItem friend, JButton button)
	{
		this.BL = BL;
		this.gui = gui;
		this.character = character;
		this.friend = friend;
		this.button = button;
		taskNumber = REQUEST_TASK;
	}
	
	public void setApproveTask(IBL BL, GUI gui, FriendRequests friendRequests, ShortCharacterItem friend, JButton button, JButton secondButton)
	{
		this.BL = BL;
		this.gui = gui;
		this.friendRequests = friendRequests;
		this.friend = friend;
		this.button = button;
		this.secondButton = secondButton;
		taskNumber = ADD_TASK;
	}
	
	public void setRemoveTask(IBL BL, GUI gui, CharacterItem character, ShortCharacterItem friend, JButton button)
	{
		this.BL = BL;
		this.gui = gui;
		this.character = character;
		this.friend = friend;
		this.button = button;
		taskNumber = REMOVE_TASK;
	}
	
	public void setIgnoreTask(IBL BL, GUI gui, FriendRequests friendRequests, ShortCharacterItem friend, JButton button, JButton secondButton)
	{
		this.BL = BL;
		this.gui = gui;
		this.friendRequests = friendRequests;
		this.friend = friend;
		this.button = button;
		this.secondButton = secondButton;
		taskNumber = IGNORE_TASK;
	}

    @Override
    protected Object doInBackground() throws IOException, InterruptedException
    {
        response = new Response();

        if (taskNumber == REQUEST_TASK)
        {
        	BL.createNewFriendRequest(character, friend, response);
        	if (response.isIs_Success())
        	{
        		button.setText("Request Sent");
        		button.setEnabled(false);
        	}
        	else
        	{
        		errorTitle = "Error Requesting Friend";
        	}
        }
        else if (taskNumber == ADD_TASK)
        {     	
        	for (FriendRequestItem requestItem : friendRequests.getRequests())
        	{
        		if (requestItem.getSender().getId() == friend.getId())
        		{
        			friendRequestItem = requestItem;
        			break;
        		}
        	}
        	if (friendRequestItem != null)
        	{
            	BL.acceptFriendRequest(friendRequests, friendRequestItem, response);
            	if (response.isIs_Success())
            	{
                	gui.setToDraw(true);
            		gui.initFriendDisplay();
            		gui.deleteFriendRequestView();
            		button.setText("Friend Added");
            		button.setEnabled(false);
            		secondButton.setEnabled(false);
            	}
            	else
            	{
            		errorTitle = "Error Adding Friend";
            	}
        	}
        }
        else if (taskNumber == REMOVE_TASK)
        {
        	BL.deleteFriend(character, friend, response);
        	if (response.isIs_Success())
        	{
            	gui.setToDraw(true);
        		gui.initFriendDisplay();
        		button.setText("Friend Removed");
        		button.setEnabled(false);
        	}
        	else
        	{
        		errorTitle = "Error Removing Friend";
        	}
        }
        else if (taskNumber == IGNORE_TASK)
        {
        	for (FriendRequestItem requestItem : friendRequests.getRequests())
        	{
        		if (requestItem.getSender().getId() == friend.getId())
        		{
        			friendRequestItem = requestItem;
        			break;
        		}
        	}
        	if (friendRequestItem != null)
        	{
            	BL.declineFriendRequest(friendRequests, friendRequestItem, response);
            	if (response.isIs_Success())
            	{
            		gui.deleteFriendRequestView();
            		button.setText("Request Ignored");
            		button.setEnabled(false);
            		secondButton.setEnabled(false);
            	}
            	else
            	{
            		errorTitle = "Error Declining Request";
            	}
        	}
        }
        
        return null;
    }
}
